<template>
    <div>
    <div class="background">
        <img :src="imgSrc" width="100%" height="100%" alt="" />
    </div >

    <div id="login-box">
        <h1>Login</h1>

        <el-form class="form" :model="ruleForm" :rules="rules" ref="ruleForm" >
            <el-form-item class="item" prop="username">

                <el-input  v-model="ruleForm.username" placeholder="username" name="username"></el-input>
            </el-form-item>
            <el-form-item class="item" prop="password">

                <el-input  v-model="ruleForm.password" type="password" placeholder="password" name="password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>

            </el-form-item>
        </el-form>
    </div>

    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return{
                imgSrc:require('../assets/images/gg.jpg'),
                ruleForm:{
                    username:'zyz647',
                    password:'123456'
                },
                rules:{
                    username: [
                        { required: true, message: '请填写用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { min: 6, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ]
                },
                dialogVisible:false
            };

        },
        methods:{
            submitForm(formName) {
                this.$refs[formName].validate((valid) =>{
                    if(valid){
                        const _this= this;
                        this.$axios.post('/user/login',this.ruleForm).then(res=>{
                            console.log(res.data)
                            const jwt=res.headers['authorization']
                            const userInfo = res.data.data;
                            console.log(userInfo)
                            _this.$store.commit("SET_TOKEN",jwt);
                            _this.$store.commit("SET_USERINFO",userInfo)

                            console.log(_this.$store.getters.getUser)
                            _this.$router.push('/main')
                        })
                    }else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .background{
        width:100%;
        height:100%;  /**宽高100%是为了图片铺满屏幕 */
        z-index:-1;
        position: absolute;
    }
    #login-box {
        z-index:1;
        position: absolute;
    }
    #login-box{
        width: 26%;
        height: auto;
        margin:0 35%;
        margin-top: 15%;
        color: #fff;
        text-align:center; /*板块内容居中*/
        background: #00000060;/*透明六位数后面两位相当于控制透明度*/
        padding:20px 30px;/*padding 内边距*/
    }
    #login-box .form{
        margin-top: 40px;

    }#login-box .form .item{
         margin-top:15px;
             width: 70%;
             margin:0 16%;
         }
    #login-box .form .item{
        margin-top:15px;
    }
    #login-box .form .item input{
        width:220px;
        font-size:18px;
        border: 0;
        border-bottom: 2px solid #fff;/*下划线*/
        padding: 5px 15px;
        background: #ffffff00;
        color: #fff;
    }
    #login-box .form .item1 input{
        width:50px;
        font-size:18px;
        border: 0;
        border-bottom: 2px solid #fff;/*下划线*/
        padding: 5px 15px;
        background: #ffffff00;
        color: #fff;
    }
    #login-box button{
        margin-top:20px;
        width:150px;
        height:35px;
        font-size:20px;
        font-weight:700;
        color:#fff;
        background-image: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
        border:0;
        border-radius:15px;
    }
</style>